package com.sign2024.sign2024project.projectservice.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sign2024.sign2024core.common.CommonResult;
import com.sign2024.sign2024core.common.annotation.Log;
import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.projectservice.RecordCollectTemp;
import com.sign2024.sign2024core.entry.projecttask.ProjectTask;
import com.sign2024.sign2024project.aop.listener.RecordCollectExcelListener;
import com.sign2024.sign2024project.projectservice.param.RecordCollectParam;
import com.sign2024.sign2024project.projectservice.service.RecordCollectService;
import com.sign2024.sign2024project.projecttask.service.ProjectTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 获取记录汇总
 *
 * @author yujie
 * @data 2024/7/26 10:44
 */
//@Api(tags = "获取记录汇总")
@Api(tags = "工程服务")
@RestController
@RequestMapping("/api/recordCollect")
public class RecordCollectController {

    @Resource
    private RecordCollectService recordCollectService;

    @Resource
    private ProjectTaskService projectTaskService;


    @Log(title = "工程服务", businessType = "导入")
    @ApiOperation(value = "获取记录汇总excel导入", notes = "获取记录汇总excel导入")
    @Transactional
    @PostMapping("/import")
    public CommonResult<RecordCollectTemp> importInfo(MultipartFile file, Integer taskId) {
        if (StringUtils.isNull(taskId)) {
            throw new CommonException(CommonConstants.ERROR, "任务标识为空");
        }

        // 删除该任务下的数据
        recordCollectService.deleteTempById(taskId);
        // 将 任务 id 进行赋值
        RecordCollectExcelListener listener = new RecordCollectExcelListener();
        listener.setTaskId(taskId);

        try {
            // 表头在第五列
//            EasyExcel.read(file.getInputStream(), listener).head(RecordCollectTemp.class).sheet().headRowNumber(5).doReadSync();
            // 测试 sheet
//            EasyExcel.read(file.getInputStream(), listener).head(RecordCollectTemp.class).sheet("test11").doReadSync();
            // 改为默认表头在第一行
            EasyExcel.read(file.getInputStream(), listener).head(RecordCollectTemp.class).sheet().doReadSync();

//            // 删除之前数据
//            recordCollectService.deleteRecordCollectByTaskId(taskId);
//            // 执行完成 做数据统计
//            recordCollectService.dealWithRecordCollect(taskId);

            // 导入数据后， 将任务中的 获取记录汇总 列修改为 已导入
            ProjectTask projectTask = new ProjectTask();
            projectTask.setId(taskId);
            projectTask.setRecordCollectTemp("已导入");
            projectTaskService.updateTask(projectTask);

            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
            throw new CommonException(CommonConstants.ERROR, "excel数据导入失败");
        }
    }

    @Log(title = "工程服务", businessType = "查询")
    @ApiOperation(value = "分页查询获取记录汇总", notes = "分页查询获取记录汇总")
    @PostMapping("/page")
    public CommonResult<RecordCollectTemp> page(RecordCollectParam param) {
        Page<RecordCollectTemp> page = recordCollectService.pageRecordCollectTemp(param);
        return CommonResult.success(page);
    }
}
